<?php
$type = [
    1 => '样品',
    2 => '快递',
    3 => '其他',
    4 => '大货',
];
?>
<?php use yii\widgets\LinkPager;?>
<?php echo $this->render('/public/header'); ?>
    <script src="https://cdn.bootcss.com/element-ui/2.0.11/index.js"></script>
    <link href="https://cdn.bootcss.com/element-ui/2.0.11/theme-chalk/index.css" rel="stylesheet">
    <div class="col-lg-12" id="main">
        <div class="col-lg-12">
            <button class="btn btn-success" @click="generateOrder">添加</button>
        </div>
        <div style="margin-top: 50px">
            <table class="table" style="text-align: center;">
                <thead>
                <tr>
                    <th>日期</th>
                    <th>业务员</th>
                    <th>报价编号</th>
                    <th>订单号</th>
                    <th>客户名称</th>
                    <th>客户编号</th>
                    <th>类型</th>
                    <th>订单支付状态</th>
                    <th>订单总金额</th>
                    <th>手续费</th>
                    <th>支付时间</th>
                    <th style="width:200px">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($list as $v): ?>
                    <tr>
                        <td><?php echo $v['create_at'] > 0 ? date('Y-m-d', $v['create_at']) : ''; ?></td>
                        <td>
                            <?php foreach ($staff as $v2): ?>
                                <?php if ($v2['job_number'] == $v['staff']): ?>
                                    <?php echo $v2['name']; ?>
                                <?php endif?>
                            <?php endforeach;?>
                        </td>
                        <td><?php echo $v['link_id']; ?></td>
                        <td><?php echo $v['order_num']; ?></td>

                        <td><?php echo $v['customer_name']; ?></td>
                        <td><?php echo $v['customer_num']; ?></td>
                        <td><?php echo $type[$v['type']];?></td>
                        <td><?php
if ($v['have_pay'] == 1):
    echo '<span class="label label-default">未支付</span>';
endif;
if ($v['have_pay'] == 2):
    echo '<span class="label label-success">已支付</span>';
endif;
if ($v['have_pay'] == 3):
    echo '<span class="label label-info">取消订单</span>';
endif;
?></td>
                        <td><?php echo $v['total_money']; ?></td>
                        <td><?php echo $v['fee']; ?></td>
                        <td><?php echo $v['pay_time'] > 0 ? date('Y-m-d', $v['pay_time']) : '--'; ?></td>
                        <td>
                            <button class="btn btn-small btn-success" @click="cancel(<?php echo $v['id']; ?>)">取消
                            </button>
                            <a href="javascript:void(0)" onclick="turnTo(<?php echo $v['order_num']; ?>)">跳转到支付链接</a>
                        </td>
                    </tr>
                <?php endforeach;?>
                </tbody>
            </table>
            <?php echo LinkPager::widget(['pagination' => $page]); ?>
        </div>
        <div>
            <el-dialog title="创建样品订单" :visible.sync="visible">
                <el-form ref="form" :model="form">
                    <el-form-item label="客户名称">
                        <el-autocomplete style="width:100%" @select="select" :fetch-suggestions="query"
                                         v-model="form.customer_name"
                                         placeholder="客户名称"></el-autocomplete>
                    </el-form-item>
                    <el-form-item label="客户编号" required>
                        <el-input v-model="form.number" placeholder="没有客户编号无法生成订单"></el-input>
                    </el-form-item>
                    <el-form-item label="绑定报价编号">
                        <el-input v-model="form.quote_id"></el-input>
                    </el-form-item>
                    <el-form-item label="订单名称">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                    <el-row :gutter="20">
                        <el-col :span="5">
                            产品名称
                        </el-col>
                        <el-col :span="5">
                            数量
                        </el-col>
                        <el-col :span="5">
                            单价
                        </el-col>
                        <el-col :span="5">
                            类型
                        </el-col>
                        <el-col :span="4">
                            删除
                        </el-col>
                    </el-row>

                    <el-row :gutter="20" v-for="(v,k) in form.item" style="margin-top: 10px">
                        <el-col :span="5">
                            <el-input v-model="v.item" placeholder=""></el-input>
                        </el-col>
                        <el-col :span="5">
                            <el-input v-model="v.num" type="number"></el-input>
                        </el-col>
                        <el-col :span="5">
                            <el-input v-model="v.price" type="number"></el-input>
                        </el-col>
                        <el-col :span="5">
                            <el-select v-model="v.type">
                                <el-option key=1 value=1 label="样品费"></el-option>
                                <el-option key=2 value=2 label="快递费"></el-option>
                                <el-option key=3 value=3 label="其他"></el-option>
                                <el-option key=4 value=4 label="大货"></el-option>
                            </el-select>
                        </el-col>
                        <el-col :span="4">
                            <el-button @click="del(k)">删除</el-button>
                        </el-col>
                    </el-row>
                    <el-button @click="addmore()" type="danger" style="margin-top: 10px">添加产品</el-button>
                    <el-form-item label="订单备注">
                        <el-input autosize type="textarea" v-model="form.remark" placeholder=""></el-input>
                    </el-form-item>
                    <el-form-item label="订单金额" required>
                        <el-input type="number" v-model="form.total_money"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submit()">生成订单</el-button>
                    </el-form-item>
                </el-form>
            </el-dialog>
        </div>
    </div>
    <script>
        new Vue({
            el: "#main",
            data: {
                visible: false,
                form: {
                    quote_id: '',
                    name: '',
                    remark: '',
                    number: '',
                    customer_name: '',
                    quote_id: '',
                    item: [
                        {
                            item: '',
                            num: 1,
                            price: 0,
                            type: "1"
                        }
                    ]
                }
            },
            watch: {
                form: {
                    handler() {
                        let a = 0;
                        if (typeof this.form.item == 'object') {
                            this.form.item.forEach(v => {
                                a += v.price * v.num;
                            });
                            this.form.total_money = a.toFixed(2);
                        }

                    },
                    deep: true,
                }
            },
            methods: {
                select(v) {
                    let tmp = this.form;
                    tmp.number = v.number;
                    tmp.customer_name = v.customer_name;
                    this.form = tmp;
                },
                query(str, cb) {
                    $.post('/api/search_customer_by_like', {name: str}, function (res) {
                        if (res.data) {
                            cb(res.data);
                        }
                    }, 'json');
                },
                del(k) {
                    let arr = [];
                    this.form.item.forEach((v, k2) => {
                        if (k != k2) {
                            arr.push(v);
                        }
                    });
                    this.form.item = arr;
                },
                addmore() {
                    const data = {
                        item: '',
                        price: 0,
                        num: 1,
                        type: "1"
                    };
                    this.form.item.push(data);
                    console.log(this.form.item);
                },
                submit() {
                    if (!this.form.total_money || !this.form.number) {
                        this.$message.error('信息未填完整');
                        return false;
                    }
                    if (this.form.total_money === 0) {
                        this.$message.error("总金额不能为0");
                        return false;
                    }
                    const that = this;
                    this.$confirm('确认生成订单')
                        .then(() => {
                            let data = this.form;
                            data.items = JSON.stringify(data.item);
                            $.post('/api/order/generate-sample-order', data, function (res) {
                                if (res.code == 1) {
                                    that.$message({
                                        type: 'success',
                                        message: '生成成功'
                                    });
                                    that.visible = false;
                                } else {
                                    that.$message.error('生成失败,请重试');
                                }
                            }, 'json');
                            console.log(this.form);
                        });

                },
                cancel(id) {
                    const that = this;
                    this.$confirm('确认取消订单').then(() => {
                        $.post('/api/order/cancel-sample', {id: id}, (res) => {
                            if (res.code == 1) {
                                that.$message({
                                    type: 'success',
                                    message: res.msg
                                });
                                window.location.reload();
                            } else {
                                that.$message.error(res.msg);
                            }

                        }, 'json')
                    })
                },
                generateOrder() {
                    this.form.total_money = '';
                    this.form.name = '';
                    this.form.remark = '';
                    this.form.item = [{
                        item: '',
                        price: 0,
                        num: 1,
                        type: "1"
                    }];
                    this.visible = true;
                }
            }

        });
function turnTo(orderNum) {
    var w = window.open();
    $.post('/api/payment/paypal-link',{order_num:orderNum},function(res){
        if (res.code==1) {
            w.location.href = res.url;
        }
    },'json')
}
    </script>

<?php echo $this->render('/public/footer'); ?>